package com.trimps.steps.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hotfire.template.common.Paginator;
import com.trimps.steps.exception.SqlOperationsException;
import com.trimps.steps.mapper.WalkerMapper;
import com.trimps.steps.pojo.Walker;
import com.trimps.steps.service.IWalkerService;

@Service("walkerService")
public class WalkerServiceImpl implements IWalkerService {
	@Autowired
	private WalkerMapper walkerMapper;	
	
	@Override
	public List<Walker> getWalker(Paginator page) {
		return getWalker(page, new HashMap<String, String>());
	}

    @Override
    public int getWalkerCount() {
        return getWalkerCount(new HashMap<String, String>());
    }

    @Override
    public List<Walker> getAllWalkers() {
        return getWalker(Paginator.generateDefaltInstance());
    }

    @Override
    public List<Walker> getWalkersByGroupId(int id) {
        return getWalkersByGroupId(id, Paginator.generateDefaltInstance());
    }

    @Override
    public List<Walker> getWalkersByGroupId(int id, Paginator page) {
        return walkerMapper.getWalkersByGroupId(id, Paginator.generateDefaltInstance());
    }

    @Override
    public int getWalkerCount(HashMap<String, String> map) {
        return walkerMapper.getWalkerCount(map);
    }

    @Override
    public List<Walker> getWalker(Paginator page, HashMap<String, String> map) {
        return walkerMapper.getWalker(page, map);
    }

    @Override
    public int save(Walker walker) throws SqlOperationsException {
        HashMap<String, String> queryMap = new HashMap<String, String>();
        queryMap.put("realyName", walker.getRealyName());
        List<Walker> walkers = walkerMapper.getWalker(Paginator.generateDefaltInstance(), queryMap);
        if(walkers != null && walkers.size() > 0)
            throw new SqlOperationsException("用户已经存在，不能重复添加！");
        return walkerMapper.save(walker);
    }

    @Override
    public int delById(int id) {
        return walkerMapper.delById(id);
    }

    @Override
    public Walker getWalkerByRealyName(String realyName) {
        HashMap<String, String> queryMap = new HashMap<String, String>();
        queryMap.put("realyName", realyName);
        List<Walker> list = getWalker(Paginator.generateDefaltInstance(), queryMap);
        if(list != null && list.size() == 1){
            return list.get(0);
        }
        return null;
    }

}
